package ru.ipo.dces.problemsviewer;

import ru.ipo.dces.utils.ResultUtils;

import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: ilya
 * Date: 09.06.11
 * Time: 19:35
 */
public class ProblemSubmissionHistory {

    private final DateFormat format = DateFormat.getTimeInstance(DateFormat.MEDIUM);

    private static class HistoryEntry {

        private Map<String, String> submission;
        private Map<String, String> checkerResult;
        private Date timeStamp;

        public HistoryEntry(Map<String, String> submission, Map<String, String> checkerResult) {
            this.submission = submission;
            this.checkerResult = checkerResult;
            this.timeStamp = new Date();
        }

        public HistoryEntry(Map<String, String> submission, Map<String, String> checkerResult, Date timeStamp) {
            this.submission = submission;
            this.checkerResult = checkerResult;
            this.timeStamp = timeStamp;
        }

        public Map<String, String> getSubmission() {
            return submission;
        }

        public Map<String, String> getCheckerResult() {
            return checkerResult;
        }

        public Date getTimeStamp() {
            return timeStamp;
        }
    }

    private class HistoryTableModel extends DefaultTableModel {

        public int getRowCount() {
            return history.size();
        }

        public int getColumnCount() {
            return 4;
        }

        public Object getValueAt(int rowIndex, int columnIndex) {
            HistoryEntry historyEntry = history.get(rowIndex);
            switch (columnIndex) {
                case 0:
                    //TODO align to center
                    return "<html>&nbsp;" + (rowIndex + 1) + "</html>";
                case 1:
                    return format.format(historyEntry.getTimeStamp());
                case 2:
                    return ResultUtils.map2string(historyEntry.getSubmission());
                case 3:
                    return ResultUtils.map2string(historyEntry.getCheckerResult());
            }
            return null;
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
                case 0:
                    return "№";
                case 1:
                    return "Время";
                case 2:
                    return "Послано";
                case 3:
                    return "Результат";
            }
            return null;
        }

        @Override
        public Class<?> getColumnClass(int columnIndex) {
            return String.class;
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            return false;
        }
    }

    private ArrayList<HistoryEntry> history = new ArrayList<HistoryEntry>();
    private HistoryTableModel tableModel = new HistoryTableModel();

    public void addEntry(Map<String, String> submission, Map<String, String> check) {
        addEntry(submission, check, new Date());
    }

    public void addEntry(Map<String, String> submission, Map<String, String> check, Date timeStamp) {
        history.add(new HistoryEntry(submission, check, timeStamp));
        int newLineIndex = history.size() - 1;
        tableModel.fireTableRowsInserted(newLineIndex, newLineIndex);
    }

    public TableModel getTableModel() {
        return tableModel;
    }

}
